<?php

namespace App\Models;

use App\Util\Tool;
use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
    protected $table ="c_addresses";

    /****
     * @param $data
     * @return bool
     * 添加地址
     */
    public function addAddress($data)
    {
        if(isset($data['status']) && $data['status'] ==1){
            $this->where([['user_id','=',$data['user_id']]])->update(['status'=>0]);
        }
        if (isset($data['id']) && !empty($data['id'])) {
            $redata = $this->getInfo($data['id']);
            $redata->status = isset($data['status'])?$data['status']:$redata->status;
            $redata->name = isset($data['name'])?$data['name']:$redata->name;
            $redata->phone = isset($data['phone'])?$data['phone']:$redata->phone;
            $redata->user_id = isset($data['user_id'])?$data['user_id']:$redata->user_id;
            $redata->detail = isset($data['detail'])?$data['detail']:$redata->detail;
            $redata->province_id = isset($data['province_id'])?$data['province_id']:$redata->province_id;
            $redata->city_id = isset($data['city_id'])?$data['city_id']:$redata->city_id;
            $redata->district_id = isset($data['county_id'])?$data['county_id']:$redata->district_id;
            $res = $redata->save();
        } else {
            //添加
            $this->status = isset($data['status'])?$data['status']:0;
            $this->name = $data['name'];
            $this->phone = $data['phone'];
            $this->user_id = $data['user_id'];
            $this->detail = $data['detail'];
            $this->province_id = $data['province_id'];
            $this->city_id = $data['city_id'];
            $this->district_id = $data['county_id'];
            $res = $this->save();
        }
        return $res;
    }

    /***
     * @param $data
     * @return mixed
     * 获取详情
     */
    public function getInfo($id)
    {
        return $this->where([['id', '=', $id]])->first();
    }

   /***
     * @param $data
     * @return mixed
     * 获取详情
     */
    public function getAddressInfo($id)
    {
        $china_addres = new Area();
        $res = $this->where([['id', '=', $id]])->first()->toArray();
        $res['province'] = $china_addres::getNameByCode($res['province_id']);
        $res['city'] = $china_addres::getNameByCode($res['city_id']);
        $res['district'] = $china_addres::getNameByCode($res['district_id']);
        return $res;
    }


    /****
     * @param $data
     * 获取地址列表
     */
    public function getAddressList($param)
    {
        $where = [];
        if (isset($param['start']) && !empty($param['start'])) {  //开始时间
            $where[] = ['ins_times', '>=', $param['start']];
        }
        if (isset($param['end']) && !empty($param['end'])) {  //开始时间
            $where[] = ['ins_times', '<=', $param['end']];
        }
        if (isset($param['status']) && !empty($param['status'])) {  //开始时间
            $where[] = ['status', '=', 1];
        }
        $list = $this->where([['user_id', '=', $param['user_id']]])->where($where)->orderByDesc("id")->get();
        if (!empty($list)) {
            $list = $list->toArray();
            $china_addres = new Area();
            foreach ($list as $key=>$value){
                $list[$key]['province']  =$china_addres::getNameByCode($value['province_id']);
                $list[$key]['city']  =$china_addres::getNameByCode($value['city_id']);
                $list[$key]['district']  =$china_addres::getNameByCode($value['district_id']);
            }
            return $list;
        }
        return [];
    }

    /***
     * @param $list
     * @return array
     * 分组排序
     */
    public static function array_group_by($list)
    {
        $newArray = array();
        foreach ($list as $v) {
            $newArray[$v['ins_times']][] = $v;
        }
        return $newArray;
    }

    /***
     * @param $data
     * @return int
     * 删除备忘录
     */
    public function delAddress($data)
    {
        return $this::destroy($data['id']);
    }
}
